package com.zzyl.nursing.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.dto.DeviceDataDto;
import com.zzyl.nursing.dto.FamilyMemberElderDto;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.DeviceDataVo;
import com.zzyl.nursing.vo.FamilyMemberElderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.FamilyMemberElderMapper;
import com.zzyl.nursing.domain.FamilyMemberElder;
import com.zzyl.nursing.service.IFamilyMemberElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.Elder;

import java.util.Arrays;

/**
 * 客户老人关联Service业务层处理
 *
 * @author ruoyi
 * @date 2025-01-10
 */
@Service
public class FamilyMemberElderServiceImpl extends ServiceImpl<FamilyMemberElderMapper, FamilyMemberElder> implements IFamilyMemberElderService {
    @Autowired
    private FamilyMemberElderMapper familyMemberElderMapper;

    @Autowired
    private IElderService elderService;

    /**
     * 查询客户老人关联
     *
     * @param id 客户老人关联主键
     * @return 客户老人关联
     */
    @Override
    public FamilyMemberElder selectFamilyMemberElderById(Long id) {
        return getById(id);
    }

    /**
     * 查询客户老人关联列表
     *
     * @param familyMemberElderDto 客户老人关联
     * @return 客户老人关联
     */
    @Override
    public List<FamilyMemberElderVo> selectFamilyMemberElderList(FamilyMemberElderDto familyMemberElderDto) {
        return familyMemberElderMapper.selectFamilyMemberElderList(UserThreadLocal.getUserId());
    }

    /**
     * 新增客户老人关联
     *
     * @param familyMemberElderDto 客户老人关联
     * @return 结果
     */
    @Override
    public boolean insertFamilyMemberElder(FamilyMemberElderDto familyMemberElderDto) {
        Elder elder = elderService.lambdaQuery().eq(Elder::getIdCardNo, familyMemberElderDto.getIdCard())
                .eq(Elder::getName, familyMemberElderDto.getName())
                .in(Elder::getStatus, 1, 2, 3, 4).one();
        if (elder == null){
            throw new  ServiceException("该老人不存在");
        }
        FamilyMemberElder familyMemberElder = FamilyMemberElder
                .builder()
                .familyMemberId(UserThreadLocal.getUserId())
                .elderId(elder.getId())
                .remark(familyMemberElderDto.getRemark())
                .build();
        return save(familyMemberElder);
    }

    /**
     * 修改客户老人关联
     *
     * @param familyMemberElder 客户老人关联
     * @return 结果
     */
    @Override
    public int updateFamilyMemberElder(FamilyMemberElder familyMemberElder) {
        return updateById(familyMemberElder) ? 1 : 0;
    }

    /**
     * 批量删除客户老人关联
     *
     * @param ids 需要删除的客户老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除客户老人关联信息
     *
     * @param id 客户老人关联主键
     * @return 结果
     */
    @Override
    public int deleteFamilyMemberElderById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<FamilyMemberElderVo> selectMy() {
        List<FamilyMemberElderVo> list = familyMemberElderMapper.getMy(UserThreadLocal.getUserId());
        return list;
    }
}
